package com.whl.sm.controller.expense;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.expense.MaterBill;
import com.whl.sm.model.expense.MaterRecord;
import com.whl.sm.model.inhabitant.AssetsOwner;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.expense.MaterBillService;
import com.whl.sm.service.expense.MaterRecordService;
import com.whl.sm.service.inhabitant.AssetsOwnerService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.vo.MaterBillRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-18
 */
@RestController
@RequestMapping("/mater-bill")
public class MaterBillController extends BaseController {

    @Autowired
    private MaterBillService materBillService;

    @Autowired
    private ComUnitsService comUnitsService;

    @Autowired
    private MaterRecordService materRecordService;

    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private OrgUserInfoService orgUserInfoService;

    @Autowired
    private AssetsOwnerService assetsOwnerService;

    @Autowired
    private AssetsTenantService assetsTenantService;

    @RequestMapping("/list")
    public Result getMaterBillList(HttpServletRequest request){
        QueryWrapper queryWrapper = new QueryWrapper();
        setHttpRequestQueryWrapper(queryWrapper,"unit_id","mater_type","payment_status");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> billPage = materBillService.page(getPage(),queryWrapper);
        List<Object> resultData = billPage.getRecords().stream().map(obj->{
            MaterBill materBill = (MaterBill) obj;
            ComUnits comUnits = comUnitsService.getById(materBill.getUnitId());
            MaterRecord materRecord  = materRecordService.getById(materBill.getMaterRecordId());
            OrgUserInfo orgUserInfo = new OrgUserInfo();
            if (materBill.getPayer() != null){
                orgUserInfo = orgUserInfoService.getByUserId(materBill.getPayer());
            }
            return new MaterBillRecordVo(materBill,comUnits,materRecord,orgUserInfo);
        }).collect(Collectors.toList());
        billPage.setRecords(resultData);
        return Result.ok(billPage);
    }

    @RequestMapping("/getByCurrent")
    public Result getByCurrent(@RequestParam("roleInfo") String roleInfo , HttpServletRequest request){
        String userId = orgUserService.getCurrentUserId();
        String linkUnit = null;
        if (roleInfo.equals("小区住户") || roleInfo.equals("小区业主")){
            List<AssetsOwner> owners = assetsOwnerService.list(new QueryWrapper<AssetsOwner>().eq("user_id",userId));
            linkUnit = owners.size()>0?owners.get(0).getAssetsId():null;
        }else if( roleInfo.equals("小区租户")){
            List<AssetsTenant> tenants = assetsTenantService.list(new QueryWrapper<AssetsTenant>().eq("user_id",userId));
            linkUnit = tenants.size()>0?tenants.get(0).getAssetsId():null;
        }
        if (Objects.isNull(linkUnit)){
            return Result.fail();
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        setHttpRequestQueryWrapper(queryWrapper,"mater_type","payment_status");
        queryWrapper.eq("unit_id",linkUnit);
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> billPage = materBillService.page(getPage(),queryWrapper);
        List<Object> resultData = billPage.getRecords().stream().map(obj->{
            MaterBill materBill = (MaterBill) obj;
            ComUnits comUnits = comUnitsService.getById(materBill.getUnitId());
            MaterRecord materRecord  = materRecordService.getById(materBill.getMaterRecordId());
            OrgUserInfo orgUserInfo = new OrgUserInfo();
            if (materBill.getPayer() != null){
                orgUserInfo = orgUserInfoService.getByUserId(materBill.getPayer());
            }
            return new MaterBillRecordVo(materBill,comUnits,materRecord,orgUserInfo);
        }).collect(Collectors.toList());
        billPage.setRecords(resultData);
        return Result.ok(billPage);
    }

    @RequestMapping("/info/{id}")
    public Result materBillInfo(@PathVariable("id") String id){
        MaterBill materBill = materBillService.getById(id);
        return Result.ok(materBill);
    }


    @RequestMapping("/payBill")
    public Result payBill(@RequestParam("billId")String billId){
        return materBillService.payBill(billId);

    }


    @RequestMapping("/update")
    public Result updateMaterBill(@RequestBody MaterBill materBill){
        return materBillService.update(materBill);

    }

    @RequestMapping("/sendMessage")
    public Result sendMessage(@RequestBody List<String> idList){
        return materBillService.sendMessage(idList);
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            materBillService.update(new UpdateWrapper<MaterBill>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

}
